package ncme.auto.framework.core.entities.aspect.parameter;

import ncme.auto.framework.core.exceptions.AutoException;
import ncme.auto.framework.core.exceptions.ExceptionEnum;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParamConverter {

    private Map<String, Object> paramMap;

    public Map<String, Object> getParamMap() {
        return paramMap;
    }

    public ParamConverter(Map<String, Object> paramMap) {
        this.paramMap = paramMap;
    }

    public boolean containsKey(String key) {
        return paramMap.containsKey(key);
    }

    public Object get(String key) {
        return paramMap.get(key);
    }

    public Integer getInt(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        try {
            return Integer.parseInt(obj.toString());
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public String getString(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        try {
            return obj.toString();
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public long getLong(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        try {
            return Long.parseLong(obj.toString());
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public MultipartFile getMultipartFile(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        try {
            return (MultipartFile) obj;
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public Date getDateSimple(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(obj.toString());
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public Date getDateIntegrate(String fieldName) {
        Object obj = this.paramMap.get(fieldName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(obj.toString());
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public <T> List<T> getArrayList(String fieldName, Class<T> classType) {
        Object obj = this.paramMap.get(fieldName);
        try {
            return (List<T>) obj;
        } catch (Throwable e) {
            throw new AutoException(ExceptionEnum.FIELD_VALUE, fieldName + "has error");
        }
    }

    public void putFilterKeyValue(String key, Object value) {
        Object filterMap1 = this.paramMap.get("filterMap");
        if (filterMap1 == null) {
            Map<String, Object> filterMap = new HashMap<>();
            filterMap.put(key, value);
            this.paramMap.put("filterMap", filterMap);
        } else {
            ((Map<String, Object>) filterMap1).put(key, value);
        }
    }

    public void replace(String key, Object value) {
        this.paramMap.replace(key, value);
    }

}
